// RSSParser.cpp: implementation of the RSSParser class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TinyPP.h"
#include "RSSParser.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

RSSParser::RSSParser()
{
}

RSSParser::~RSSParser()
{

}

bool RSSParser::ParseFile(const char* filename,RSSFeed *pFeed)
{
	Reset();
	m_pFeed = pFeed;
	m_pFeed->m_Items.clear();
	
	TiXmlDocument doc(filename);
	if(!doc.LoadFile()){
		//MessageBox(NULL,doc.ErrorDesc(),"failed in parsing RSS file",MB_OK);
		return false;
	}
	return Parse(doc);
	
}

bool RSSParser::Parse(TiXmlDocument &doc)
{
	m_pFeed->m_strEncoding="utf-8"; //default is utf-8
	TiXmlNode* declarationNode=doc.FirstChild();
	if(declarationNode){
		TiXmlDeclaration* declaration = declarationNode->ToDeclaration();
		if(declaration){
			const char* strTemp=declaration->Encoding();
			if(strTemp && strTemp[0]){ // if has the encoding declaration
				m_pFeed->m_strEncoding=strTemp;
			}
		}		
	}

	TiXmlHandle h(doc.RootElement());
	TiXmlElement* root=h.Element();
	if(root){
		const char* strTemp = root->Attribute("version");
		if(strTemp){
			m_pFeed->m_strVersion=strTemp;
		}
	}else{
		return false;
	}

	if (m_pFeed->m_strVersion.empty()) {
		m_pFeed->m_strVersion="1.0";
	}

	TiXmlHandle channel(h.FirstChild("channel").Element());
	if(channel.Element() == NULL)
		return false;

	TiXmlText* title_elem = channel.FirstChild("title").FirstChild().Text();
	if(title_elem){
		const char* strTemp = title_elem->Value();
		if(strTemp){
			m_pFeed->m_strChannelTitle=m_pFeed->GetEncoding()=="utf-8"?UTF8_to_ANSI(strTemp):strTemp;
		}
	}

	TiXmlText* link_elem = channel.FirstChildElement("link").FirstChild().Text();
	if(link_elem){
		const char* strTemp = link_elem->Value();
		if(strTemp){
			m_pFeed->m_strChannelLink=strTemp;
		}		
	}

	string strDateELemText="pubDate";
	if(m_pFeed->m_strVersion == "1.0"){
		strDateELemText="dc:date";
	}

	TiXmlText* pubdate_elem = channel.FirstChildElement(strDateELemText.c_str()).FirstChild().Text();
	if(pubdate_elem){
		const char* strTemp = pubdate_elem->Value();
		if(strTemp){
			m_pFeed->m_strChannelPubdate=strTemp;
		}
	}	
	
	TiXmlElement* items=channel.Element();
	if(m_pFeed->m_strVersion == "1.0"){
		items=root;
	}

	TiXmlNode* item=NULL;	
	while(item = items->IterateChildren("item",item)){
		TiXmlHandle i(item);
		string title;
		string link;
		string description;

		TiXmlText* title_elem=i.FirstChild("title").FirstChild().Text();
		if(title_elem){
			const char* strTemp=title_elem->Value();
			if(strTemp){
				title=m_pFeed->GetEncoding()=="utf-8"?strTemp:ANSI_to_UTF8(strTemp);
			}
		}

		TiXmlText* link_elem=i.FirstChild("link").FirstChild().Text();
		if(link_elem){
			const char* strTemp=link_elem->Value();
			if(strTemp){
				link=m_pFeed->GetEncoding()=="utf-8"?strTemp:ANSI_to_UTF8(strTemp);
			}
		}

		TiXmlText* description_elem=i.FirstChild("description").FirstChild().Text();
		if(description_elem){
			const char* strTemp=description_elem->Value();
			if(strTemp){
				description=m_pFeed->GetEncoding()=="utf-8"?strTemp:ANSI_to_UTF8(strTemp);
			}
		}

		if(title=="")
			title="no title";


		m_pFeed->m_Items.push_back(RSSItem(title,link,description));
	}
	
	return true;
}

void RSSParser::Reset()
{
	m_pFeed=NULL;
}
